package com.fjwt.gz.mch.ctrl.goods;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.PointDTO;
import com.fjwt.gz.db.vo.AgentDestroyVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AwardCouponService;
import com.fjwt.gz.service.impl.AwardGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/api/award/goods")
public class AwardGoodsController extends CommonCtrl {

    @Autowired
    private AwardGoodsService awardGoodsService;

    @Autowired
    private AwardCouponService awardCouponService;

    /**
     * 实物奖品导航至使用机构的机构列表
     **/
    @PostMapping(value = "/getAgentDestroyList")
    public ApiRes getNearAgentList() {
        PointDTO location = getVal("location", PointDTO.class);
        Long goodsId = getValLong("goodsId");
        Long couponId = getValLong("couponId");
        String radius = getValString("radius");
        log.info("C端奖品导航至使用机构列表接口调用，请求地址：/api/award/goods/getAgentDestroyList，请求参数：奖品id：{}，卡券id：{}，位置{}，范围单位（米）：{}",
                goodsId, couponId, location, radius);
        if (location == null || (Objects.isNull(couponId) && Objects.isNull(goodsId))) {
            return ApiRes.customFail("网络故障，请稍后重试！");
        }
        IPage<AgentDestroyVO> list;
        if (Objects.isNull(couponId)) {
            list = awardGoodsService.getAgentDestroyList(getIPage(), location, goodsId, radius);
        } else {
            list = awardCouponService.getAgentDestroyList(getIPage(), location, couponId, radius);
        }
        log.info("C端奖品导航至使用机构列表接口调用，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.page(list);
    }
}
